package com.example.democommon.tools;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import com.example.democommon.contasant.CommonConstant;
import com.github.javafaker.Faker;
import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;

import java.security.SecureRandom;
import java.util.Locale;
import java.util.Random;
import java.util.UUID;

/**
 * @Description: ID
 * @Title: IDUtil
 * @Package com.example.democommon.tools
 * @Author: rosszhang
 * @Date: 2022/4/26 12:01
 * @Version 1.0
 */
public class IDUtil {
    public static final SecureRandom DEFAULT_NUMBER_GENERATOR = new SecureRandom();
    public static final char[] DEFAULT_ALPHABET = "_-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
    public static final int DEFAULT_SIZE = 21;

    private IDUtil() {
    }

    public static String randomNanoId() {
        return randomNanoId(DEFAULT_NUMBER_GENERATOR, DEFAULT_ALPHABET, DEFAULT_SIZE);
    }

    public static String randomNanoId(Random random, char[] alphabet, int size) {
        if (random == null) {
            throw new IllegalArgumentException("random cannot be null.");
        } else if (alphabet == null) {
            throw new IllegalArgumentException("alphabet cannot be null.");
        } else if (alphabet.length != 0 && alphabet.length < 256) {
            if (size <= 0) {
                throw new IllegalArgumentException("size must be greater than zero.");
            } else {
                int mask = (2 << (int) Math.floor(Math.log((double) (alphabet.length - 1)) / Math.log(2.0D))) - 1;
                int step = (int) Math.ceil(1.6D * (double) mask * (double) size / (double) alphabet.length);
                StringBuilder idBuilder = new StringBuilder();

                while (true) {
                    byte[] bytes = new byte[step];
                    random.nextBytes(bytes);

                    for (int i = 0; i < step; ++i) {
                        int alphabetIndex = bytes[i] & mask;
                        if (alphabetIndex < alphabet.length) {
                            idBuilder.append(alphabet[alphabetIndex]);
                            if (idBuilder.length() == size) {
                                return idBuilder.toString();
                            }
                        }
                    }
                }
            }
        } else {
            throw new IllegalArgumentException("alphabet must contain between 1 and 255 symbols.");
        }
    }


    public static void main(String[] args) {
        /*Long naoID = System.currentTimeMillis();
        Long uuID = System.currentTimeMillis();
        System.out.println("开始时间：" + naoID);
        for (int i = 0; i < 10; i++) {
            System.out.println(UUID.randomUUID());
            String str = CommonConstant.ACCOUNT;
            //System.out.println(IDUtil.randomNanoId());
            //33
            //D:\rosswork-bj\java-wj\demo-one\demo-common\src\main\java\com\example\democommon\contasant\CommonConstant.java
            System.out.println(UUID.randomUUID());
            //268
        }
        //DateUtil.formatBetween();
        //IdUtil
        System.out.println("结束时间：" + (System.currentTimeMillis() - naoID));*/
        Faker faker = new Faker(Locale.CHINA);
        System.out.println(RandomUtil.randomInt(1,20));
        System.out.println(faker.book().author());
        System.out.println(faker.address().cityName());
        System.out.println(faker.address().streetAddress());   //街道
        System.out.println(faker.university().name());
        System.out.println(DateUtil.format(faker.date().birthday(), DatePattern.NORM_DATE_FORMAT));
    }
}
